<template>
  <div style="width: 100%">
    <a-card class="card" :bordered="false" >
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="招生学年">
              <a-select :allowClear="true" showSearch optionFilterProp="children"  v-model="queryParam.zsxn">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:16}" :xl="{span:8}">
            <a-button type="primary" @click="loadData" style="margin-top:29px">查询</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
  </a-card >    
    <a-card title="考生性别统计图" :bordered="false">
      <div id="mountNode"></div>
    </a-card>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import G2 from '@antv/g2'
const selectArr = ['TYPE0126']
export default {
  name: 'Gender',
  data () {
    return {
      chart:null,
      queryParam:{
        zsxn:''
      }
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  created () {
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  activated(){
    this.chart = new G2.Chart({
      container: 'mountNode',
      forceFit: true,
      height: 500
    });
    this.loadData()
  },
  deactivated(){
    this.chart.destroy()
  },
  methods: {
    loadData(){
      if(!this.queryParam.zsxn)this.queryParam.zsxn = ''
      this.$api.situation.genderList(this.queryParam).then(res => {
        res.data.forEach(item=>{
          if(item.percent){
            item.percent = parseFloat(item.percent)
          }
        })
      var data = res.data
      if(data.length){
      this.chart.clear()
      this.chart.source(data, {
            percent: {
            formatter: function formatter(val) {
              val = (val * 100).toFixed(1) + '%';
              return val;
            }
          }
      });
      this.chart.coord('theta', {
          radius: 0.75
      });
      this.chart.tooltip({
        showTitle: false,
        itemTpl: '<li><span style="background-color:{color};" class="g2-tooltip-marker"></span>{name}: {value}</li>'
      });
      this.chart.intervalStack().position('percent').color('item').label('percent', {
        formatter: function formatter(val, item) {
          return item.point.item+': '+item.point.count+ ',占比: ' + val;
        }
      }).tooltip('item*percent', function(item, percent) {
        percent = (percent * 100).toFixed(1)+'%';
        return {
          name: item,
          value: percent
        };
      }).style({
        lineWidth: 1,
        stroke: '#fff'
      });
     this.chart.render();   
      }else{
        this.$message.warning('没有查询到数据')
        this.chart.clear();
      }
      })
    }
  }
}
</script>
<style lang='less' scoped>
.card{
  margin-bottom: 20px;
}
</style>
